﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PanhuaWin.超级管理员
{
    public partial class Form删除大标签 : UIPage
    {
        public Form删除大标签()
        {
            InitializeComponent();
        }

        int 开始行;
        int 结束行;
        int 开始列;
        int 结束列;


        private UIRadioButton 当前来源选项 = new UIRadioButton();
        private bool 是否已打印;

        private void Form删除大标签_Load(object sender, EventArgs e)
        {
            this.uiDatePicker结束日期.Value = DateTime.Today.AddDays(-1);
            uiDataGridView产出.AutoGenerateColumns = false;
            pub.ListWorkShop(pub.sys_user, this.uiComboBoxWorkShop);
            ListMaterial();
        }

        private void uiButton查询_Click_1(object sender, EventArgs e)
        {
            listData();
        }

        private void listData()
        {
            if (uiRadioButton酸洗.Checked)
            {
                queryLablePrint("酸洗");
            }

            if (uiRadioButton轧硬.Checked)
            {
                queryLablePrint("轧硬");
            }
            if (uiRadioButton镀锌.Checked)
            {
                queryLablePrint("镀锌");
            }
            if (uiRadioButton彩涂.Checked)
            {
                queryLablePrint("彩涂");
            }
            if (uiRadioButton罩退.Checked)
            {
                queryLablePrint("罩退");
            }
            if (uiRadioButton退火.Checked)
            {
                queryLablePrint("退火");
            }
            if (uiRadioButton硅钢.Checked)
            {
                queryLablePrint("硅钢");
            }
        }

        private void queryLablePrint(string 来源种类)
        {
            decimal 总重量;
            decimal 总长度;
            int 总卷数;
            总重量 = 0;
            总长度 = 0;
            string sql = @"select  guid, 来源种类, 来源产线, 来源班组, 工厂编号, 生产批号, 存货编号, 品名, 钢号, 条码, 厚度, 产地, 长度, 规格, 
                           等级, 表面状态, 表面处理, 净重, 备注, 生产日期, 检验员, 操作ip, 操作人员, 操作时间, 仓库, 货位, 锌层, 包装形式, 班组, 
                           客户, 颜色, 是否已打印, 打印次数, 是否抛送NC, 抛NC结果
                            from Bus_LablePrint where 1=1";

            if (来源种类 == "镀锌")
            {
                sql += " and (来源种类 like '%镀锌%' or 来源种类 like '%彩基板%')  ";
            }
            else
            if (来源种类 == "罩退")
            {
                sql += "and (来源种类 like '%罩退%' OR 来源种类 like '%冷轧%')";
            }
            else
            {
                sql += " and 来源种类 like '%" + 来源种类 + "%' ";
            }

            if (checkBox包含已打印.Checked)
            {
                sql += " and (是否已打印 = '' or 是否已打印 is null or 是否已打印 = '是')";
            }
            else
            {
                sql += " and (是否已打印 = '' or 是否已打印 is null)";
            }
            sql += " and 生产日期 >= '" + this.uiDatePicker开始日期.Value + "' and 生产日期 < '" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";

            if (!string.IsNullOrEmpty(uiTextBox规格型号1.Text.Trim()))
            {
                sql += " and 规格 = '" + uiTextBox规格型号1.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(uiComboBoxWorkShop.SelectedText))
            {
                sql += " and 工厂编号 = '" + uiComboBoxWorkShop.SelectedValue.ToString() + "'";
            }
            if (!string.IsNullOrEmpty(uiTextBox品名.Text.Trim()))
            {
                sql += " and 品名 = '" + uiTextBox品名.Text.Trim() + "'";
            }

            if (!string.IsNullOrEmpty(uiTextBox生产批号.Text.Trim()))
            {
                sql += " and 生产批号 like '%" + uiTextBox生产批号.Text.Trim().ToUpper() + "%'";
            }
            if (!string.IsNullOrEmpty(uiComboBox材质.Text.Trim()) && uiComboBox材质.Text.Trim() != "请选择")
            {
                sql += " and 钢号 = '" + uiComboBox材质.Text.Trim() + "'";
            }

            if (!string.IsNullOrEmpty(uiComboBox产线.Text.Trim()) && uiComboBox产线.Text.Trim() != "请选择")
            {
                sql += " and 来源产线 = '" + uiComboBox产线.Text.Trim() + "'";
            }

            //sql += " group by  [来源种类], [来源产线], [来源班组], [工厂编号], [生产批号], [存货编号], [品名], [钢号], [条码], [厚度], [产地], [长度], [规格], [等级], [表面状态], [表面处理], [净重], [备注], [生产日期], 仓库, 货位, 锌层,包装形式,客户,是否已打印, 打印次数,Bus_LablePrint.颜色 order by Bus_LablePrint.品名,规格, 生产批号 ";
            DataTable dt = pub.GetDataTable(pub.ConnectionString, sql);
            uiDataGridView产出.AutoGenerateColumns = false;
            uiDataGridView产出.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            uiDataGridView产出.DataSource = dt;
            uiDataGridView产出.Refresh();

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = false;
                    decimal.TryParse(dt.Rows[i]["净重"]?.ToString(), out decimal a);
                    decimal 净重 = a;
                    总重量 += 净重;
                    decimal.TryParse(dt.Rows[i]["长度"].ToString(), out decimal b);
                    总长度 += b;
                }
            }

            this.uiTextBox总卷数.Text = dt.Rows.Count.ToString();
            this.uiTextBox总重量.Text = Math.Round(总重量, 0).ToString();
            this.uiTextBox总米数.Text = 总长度.ToString();
        }

        private void uiRadioButton酸洗_CheckedChanged_1(object sender, EventArgs e)
        {
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }


        private void ListWorkLine(string WorkType, UIComboBox uiComboBox产线)
        {
            string sql;
            sql = @"SELECT '' WorkLineCode,'请选择' WorkLineName
                    union all
                    SELECT   WorkLineCode, WorkLineName
                    FROM      Dic_WorkLine where WorkType='" + WorkType + "' order by WorkLineCode";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            uiComboBox产线.DataSource = tb;
            uiComboBox产线.DisplayMember = "WorkLineName";
            uiComboBox产线.ValueMember = "WorkLineCode";
            uiComboBox产线.Refresh();
        }
        public void ListMaterial()
        {
            string sql;
            sql = @"  select '请选择' ProductMaterial
                    union all
                    select distinct ProductMaterial from Dic_WorkTypeMaterial";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                uiComboBox材质.DataSource = tb;
                uiComboBox材质.DisplayMember = "ProductMaterial";
                uiComboBox材质.ValueMember = "ProductMaterial";
                uiComboBox材质.Refresh();
            }
            else
            {
                uiComboBox材质.DataSource = null;

                uiComboBox材质.Refresh();
            }
        }

        private void uiRadioButton轧硬_CheckedChanged_1(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton镀锌_CheckedChanged_1(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton彩涂_CheckedChanged_1(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton罩退_CheckedChanged_1(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }
        private void uiRadioButton退火_CheckedChanged_1(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void uiRadioButton硅钢_CheckedChanged_1(object sender, EventArgs e)
        {
            当前来源选项 = sender as UIRadioButton;
            ListWorkLine((sender as UIRadioButton).Text, this.uiComboBox产线);
        }

        private void checkBox包含已打印_CheckedChanged(object sender, EventArgs e)
        {
            listData();
        }
        private void uiDataGridView产出_CellMouseDown_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            开始行 = e.RowIndex;
            //this.uiLabel开始.Text = 开始行.ToString();

            开始列 = e.ColumnIndex;
        }

        private void uiDataGridView产出_CellMouseUp_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            decimal 总重量 = 0;
            decimal 总米数 = 0;
            decimal 卷数 = 0;
            结束行 = e.RowIndex;
            结束列 = e.ColumnIndex;
            //this.uiLabel结束.Text = 结束行.ToString();
            if (开始行 == 0 && 结束行 == 0)
            {
                //选择了就取消 没选择就选择
                if (this.uiDataGridView产出.Rows[开始行].Cells["C选择"].Value != null && (bool)this.uiDataGridView产出.Rows[开始行].Cells["C选择"].Value == true)//打钩
                {
                    this.uiDataGridView产出.Rows[开始行].Cells["C选择"].Value = false;
                }
                else
                if (this.uiDataGridView产出.Rows[开始行].Cells["C选择"].Value == null || (bool)this.uiDataGridView产出.Rows[开始行].Cells["C选择"].Value == false)
                {
                    this.uiDataGridView产出.Rows[开始行].Cells["C选择"].Value = true;

                }
            }

            if (开始行 >= 0 && 结束行 > 0)
            {
                if (开始行 > 结束行)
                {
                    int k = 开始行;
                    开始行 = 结束行;
                    结束行 = k;
                }
                for (int i = 开始行; i <= 结束行; i++)
                {
                    //选择了就取消 没选择就选择
                    if (this.uiDataGridView产出.Rows[i].Cells["C选择"].Value != null && (bool)this.uiDataGridView产出.Rows[i].Cells["C选择"].Value == true)//打钩
                    {
                        this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = false;
                    }
                    else
                    if (this.uiDataGridView产出.Rows[i].Cells["C选择"].Value == null || (bool)this.uiDataGridView产出.Rows[i].Cells["C选择"].Value == false)
                    {
                        this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = true;

                    }

                    卷数++;
                    总重量 += pub.Todecimal(this.uiDataGridView产出.Rows[i].Cells["C净重"].Value.ToString());
                    总米数 += pub.Todecimal(this.uiDataGridView产出.Rows[i].Cells["C长度"].Value.ToString());
                }

                this.uiTextBox总重量.Text = 总重量.ToString();
                this.uiTextBox总米数.Text = 总米数.ToString();
                this.uiTextBox总卷数.Text = 卷数.ToString();
            }
        }


        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (uiDataGridView产出 != null && uiDataGridView产出.Rows != null && uiDataGridView产出.Rows.Count > 0)
            {
                for (int i = 0; i < uiDataGridView产出.Rows.Count; i++)
                {//选择了就取消 没选择就选择
                    this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = true;
                }
            }
        }
        private void uiButton全不选_Click(object sender, EventArgs e)
        {
            if (uiDataGridView产出 != null && uiDataGridView产出.Rows != null && uiDataGridView产出.Rows.Count > 0)
            {
                for (int i = 0; i < uiDataGridView产出.Rows.Count; i++)
                {//选择了就取消 没选择就选择
                    this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = false;
                }
            }
        }

        private void uiButton反选_Click(object sender, EventArgs e)
        {
            // 选择了就取消 没选择就选择
            if (uiDataGridView产出 != null && uiDataGridView产出.Rows != null && uiDataGridView产出.Rows.Count > 0)
            {
                for (int i = 0; i < uiDataGridView产出.Rows.Count; i++)
                {
                    if (this.uiDataGridView产出.Rows[i].Cells["C选择"].Value != null && (bool)this.uiDataGridView产出.Rows[i].Cells["C选择"].Value == true)//打钩
                    {
                        this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = false;
                    }
                    else
                    if (this.uiDataGridView产出.Rows[i].Cells["C选择"].Value == null || (bool)this.uiDataGridView产出.Rows[i].Cells["C选择"].Value == false)
                    {
                        this.uiDataGridView产出.Rows[i].Cells["C选择"].Value = true;

                    }
                }
            }
        }

        private void uiDataGridView产出_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (uiDataGridView产出.Columns[e.ColumnIndex].Name  == "C删除")
            {
                //删除
                if (MessageBox.Show("该操作不可恢复，确定删除？", "警告！！！", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string guid = uiDataGridView产出.Rows[e.RowIndex].Cells["Cguid"].Value.ToString();
                    string sql = "delete from Bus_LablePrint where guid = '"+ guid + "'";
                    int r = pub.ExecuteSql(pub.ConnectionString,sql,"删除大标签");
                    if (r <1)
                    {
                        MessageBox.Show("删除失败");
                    }
                }
            }
        }
    }
}
